package com.ws.shop.dao.impl;

import java.sql.Connection;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


import org.apache.log4j.Logger;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.stereotype.Repository;

import com.ws.shop.bean.Page;
import com.ws.shop.dao.ParticipateDao;

import com.ws.shop.util.JdbcUtil;

@Repository(value="participateDao")
public class ParticipateDaoImpl extends BaseDaoImpl implements  ParticipateDao {
	
	private static Logger log = Logger.getLogger(ParticipateDaoImpl.class);
	
	String queryfiled = "a.id,a.sn,a.gid,a.goodsname,a.goodsprice,a.goods_pic,a.starttime,a.endtime,a.ordertime,a.startprice,a.addsecond,a.addprice,a.usebeat,a.pointc,a.costprice,a.recommend,a.auctioncount,a.realprice,a.auctionuname,a.uid,a.robots,a.robotspromote,a.min,a.max,a.target,a.agoid,a.agopic,a.delaytype,"
		+"a.type,a.rebate,a.boutlimit,a.iskeep,a.beforebout,a.discount,a.isdiscount,a.isfree,a.maxaddsecond,a.state";
	
	public List<Object> getParticipateAuction(Page page, long uid,int limit){
		String cs = "select COUNT(1) from jvf_participate where uid = " + uid;
		String ds = "select "+queryfiled+" from jvf_participate as p join jvf_auction as a on p.aid = a.id where p.uid = " + uid + " order by p.id desc";
		return queryAuctionAbout(page,cs,ds,limit);
	}
	
	public List<Object> getUserAuction(Page page, long aid,int limit){
		queryfiled = "u.id,u.name,u.mail,u.password,u.realname,u.ip,u.head,u.sex,u.birthday,u.tel,u.score,u.beat,u.pointb,u.pointc,u.priority,u.robot,u.addtime,u.phone,u.old,u.uploadhead,u.state";
		String cs = "select COUNT(1) from jvf_participate where aid = " + aid;
		String ds = "select "+queryfiled+" from jvf_participate as p join jvf_user as  u on p.uid = u.id where p.aid = " + aid + " order by p.id desc ";
		return queryAuctionAbout(page,cs,ds,limit);
	}

	public List<Object> queryAuctionAbout(final Page page,final String countsql,final String datasql,final int limit){
		 return (List<Object>)this.execute(
					new ConnectionCallback() {
						public Object doInConnection(Connection connect) {
							Statement psmt = null;
							ResultSet rs = null;
							try {
								long totalnum = getCount(connect,countsql);
								if(totalnum>0){
									page.setTotalnum(totalnum);									
									psmt = connect.createStatement();
									StringBuilder bufs = new StringBuilder();
									bufs.append(datasql);
									if(limit==0){
										bufs.append(" limit ").append(page.getStartrownum()).append(",").append(page.getPagesize());
										
									}else{
										bufs.append(" limit " + limit);
									}
									rs = psmt.executeQuery(bufs.toString());
									//System.out.println(sb.toString());
									ResultSetMetaData rsm = rs.getMetaData();
									// 取得数据库的列数
									int col = rsm.getColumnCount();
									// 生成col长度的Object数组
									Object[] obj = new Object[col];
									List<Object> objlist = new ArrayList<Object>();
									// 遍历结果集，将结果存入Object数组
									while (rs.next()) {
										for (int i = 0; i < col; i++) {
											obj[i] = rs.getObject(i + 1);
										}
										if(limit==0){											
											page.addData(obj);
										}else{
											objlist.add(obj);
										}
									}
									return objlist;
								}
								return null;
							} catch (Exception e) {
								e.printStackTrace();
								log.error("queryAuctionAbout!",e);
								return null;
							} finally {
								JdbcUtil.close(rs, psmt, null);
				      		}
				}
	      });
	}
	
	
	public long getCount(Connection connect,String s) throws SQLException{
		Statement stmt = connect.createStatement();
		ResultSet r = stmt.executeQuery(s);
		long totalnum = 0;
		if (r.next()) {
			totalnum = r.getLong(1);
		}
		JdbcUtil.close(r,stmt,null);
		return totalnum;
	}
	
}
